package com.yfp.client.controller;

import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result;
import com.yfp.client.annotation.AnonymousAccess;
import com.yfp.client.domain.dto.BalancePayDTO;
import com.yfp.client.domain.dto.OrderCreateWxDTO;
import com.yfp.client.domain.vo.QrCodeInitVO;
import com.yfp.client.domain.vo.QrCodePayVO;
import com.yfp.client.service.PayService;
import com.yfp.client.util.Response;
import com.yfp.client.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * 订单
 *
 *
 * @version 1.0
 * @date 2024-05-24 10:20
 */
@RestController
@RequestMapping("/client/pay")
@Tag(name = "支付模块")
public class PayController {

    @Resource
    private PayService payService;


    @PostMapping("/weixin/create")
    @Operation(summary = "微信创建预支付交易单", description = "小程序支付创建订单使用")
    public Response<WxPayUnifiedOrderV3Result.JsapiResult> createOrder(@RequestBody OrderCreateWxDTO orderCreateWxDTO) {
        WxPayUnifiedOrderV3Result.JsapiResult order = payService.createOrder(SecurityUtils.getClient().get("openId").toString(), orderCreateWxDTO.getOrderId());
        return Response.success(order);
    }

    @AnonymousAccess
    @PostMapping("/weixin/device/create")
    @Operation(summary = "微信设备创建预支付交易单", description = "设备支付创建订单使用")
    public Response<WxPayUnifiedOrderV3Result.JsapiResult> createDeviceOrder(@RequestBody OrderCreateWxDTO orderCreateWxDTO) {
        WxPayUnifiedOrderV3Result.JsapiResult order = payService.createDeviceOrder(orderCreateWxDTO.getOpenId(), orderCreateWxDTO.getOrderId());
        return Response.success(order);
    }

    @AnonymousAccess
    @PostMapping("/weixin/notify/create")
    @Operation(summary = "微信支付回调通知处理", description = "小程序支付创建订单使用")
    public String parseOrderNotifyResult(@RequestBody String xmlData) {
        return payService.parseOrderNotifyResult(xmlData);
    }

    @AnonymousAccess
    @GetMapping("/admin/weixin/refund")
    @Operation(summary = "后台管理微信支付退款", description = "后台订单退款使用")
    public Response<Void> adminRefund(@RequestParam("orderListId") String orderListId, @RequestParam("amt") BigDecimal amt) {
        payService.adminRefund(orderListId, amt);
        return Response.success();
    }

    @AnonymousAccess
    @GetMapping("/admin/balance/refund")
    @Operation(summary = "后台管理余额支付退款", description = "后台订单退款使用，部分退款")
    public Response<Void> adminBalanceRefund(@RequestParam("orderListId") String orderListId, @RequestParam("amt") BigDecimal amt) {
        payService.adminBalanceRefund(orderListId, amt);
        return Response.success();
    }

    @AnonymousAccess
    @PostMapping("/weixin/notify/refund")
    @Operation(summary = "退款回调通知处理", description = "小程序支付创建订单使用")
    public String parseRefundNotifyResult(@RequestBody String xmlData) {
        return payService.parseRefundNotifyResult(xmlData);
    }

    @AnonymousAccess
    @GetMapping("/weixin/notify/refund/simulate")
    @Operation(summary = "退款回调通知处理-模拟请求", description = "后台订单退款使用")
    public Response<Void> parseRefundNotifyResultSimulate(@RequestParam("orderListId") String orderListId, @RequestParam("amt") BigDecimal amt) {
        payService.parseRefundNotifyResultSimulate(orderListId,amt);
        return Response.success();
    }

    @GetMapping("/wechat_pay/qr_code_page/init")
    @AnonymousAccess
    @Operation(summary = "设备二维码支付初始化", description = "支付初始化,用于获取微信应用id和预支付订单id")
    public Response<QrCodeInitVO> qrCodePayPageInit(@RequestParam("session_id") String sessionId) {
        return Response.success(payService.qrCodePayPageInit(sessionId));
    }

    @GetMapping("/wechat_pay/qr_code_page/pay_info")
    @AnonymousAccess
    @Operation(summary = "支付信息", description = "获取微信支付的信息")
    public Response<QrCodePayVO> qrCodePayPagePayInfo(@RequestParam("wx_code") String wxCode, @RequestParam("pre_pay_id") String pre_pay_id) {
        return Response.success(payService.qrCodePayPagePayInfo(wxCode, pre_pay_id));
    }

    @AnonymousAccess
    @Operation(summary = "余额支付")
    @PostMapping("/balance")
    public Response<Void> balancePay(@RequestBody BalancePayDTO balancePayDTO) throws Exception {
        // balancePayDTO.setPassword(RsaUtils.decryptByPrivateKey(balancePayDTO.getPassword()));
        payService.balancePay(balancePayDTO);
        return Response.success();
    }


    /**
     * 储值退款
     *
     * @param clientId
     * @return
     */
    @AnonymousAccess
    @Operation(summary = "储值退款")
    @PostMapping("/storedRefund")
    public Response<Void> storedRefund(@RequestParam("clientId") String clientId) {
        payService.storedRefund(clientId);
        return Response.success();
    }


    @AnonymousAccess
    @PostMapping("/weixin/notify/storedRefund")
    @Operation(summary = "储值退款回调通知处理")
    public String notifyStoredRefund(@RequestBody String xmlData) {
        return payService.notifyStoredRefund(xmlData);
    }


    @AnonymousAccess
    @Operation(summary = "商城订单退款")
    @PostMapping("/mallOrderRefund")
    public Response<Void> mallOrderRefund(@RequestParam("id") Long id){
        payService.mallOrderRefund(id);
        return Response.success();
    }

    @AnonymousAccess
    @PostMapping("/weixin/notify/mallOrderRefund")
    @Operation(summary = "商城订单退款回调通知处理")
    public String notifyMallOrderRefund(@RequestBody String xmlData) {
        return payService.notifyMallOrderRefund(xmlData);
    }



}
